﻿/******************************************************************************************
*                                                                                         *    
*                              Developed by Bruno Pires                                   *
*                                                                                         *
*                             email: bruno@blastersystems.com                             *
*                               web: www.blastersystems.com                               *
*                              blog: www.blastersystems.com/blog                          *
*                           twitter: @brunoacpires                                        *
*                                                                                         *
*             Software available under GNU LGPL v3 License without any guarantee          *
*                                                                                         *    
* *****************************************************************************************/ 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenNI;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Net;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows;

namespace Pap.Service.ImageIR
{
    public class PapIR
    {
        public WriteableBitmap StartDataProcessing(Context ctx, IRGenerator generator)
        {
            if (generator != null)
            {
                var mapMode = generator.MapOutputMode;
                return new WriteableBitmap(mapMode.XRes, mapMode.YRes, 96, 96, PixelFormats.Gray16, null);
            }
            else
            {
                throw new Exception("There is no node defined for InfraRed, veriry OpenNI config file.");
            }
        }

        public WriteableBitmap UpdateIr(IRGenerator generator, WriteableBitmap irBitmap)
        {
            var metadata = generator.GetMetaData();

            if (irBitmap != null)
            {
                // Create an array to hold translated image data.
                short[] image = new short[metadata.XRes * metadata.YRes];

                int idx = 0;

                irBitmap.Lock();

                for (int i = 0; i < metadata.XRes * metadata.YRes * 2; i += 2)
                {
                    // Read a pixel from the buffer.
                    short pixel = Marshal.ReadInt16(metadata.IRMapPtr, i);

                    // Convert to little endian.
                    pixel = IPAddress.HostToNetworkOrder(pixel);
                    image[idx++] = pixel;
                }

                irBitmap.WritePixels(new Int32Rect(0, 0, metadata.XRes, metadata.YRes), image, irBitmap.BackBufferStride, 0);
                irBitmap.Unlock();
            }

            return irBitmap;
        }
    }
}
